package com.ers.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ers.auth.HrAuthPassport;
import com.ers.model.HR;

public class HrAuthInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
			HrAuthPassport hrAuthPassport = ((HandlerMethod)handler).getMethodAnnotation(HrAuthPassport.class);
			if(hrAuthPassport == null || !hrAuthPassport.validate()){
				return true;
			} else {
				HR hr = (HR)request.getSession().getAttribute("hr");
				if(hr!=null){
					return true;
				}else {
					String path = request.getContextPath();
					response.sendRedirect(path+"/hr/login");
					return false;
					
				}
			}
		} else {
			return true;
		}
	}

	
	
}
